#include "stdafx.h"

#include "CurveManager.h"
#include "CurvePanel.h"

namespace NS_GXX{
namespace NS_GXX_GRAPH{
	
CCurveManager::CCurveManager()
{
	m_pCurvePanel = NULL;
	m_lCurveID = 0;
}
CCurveManager::~CCurveManager()
{

}

CCurveBase* CCurveManager::GetCurve( int iCurveIndex )
{
	if( iCurveIndex >= (int)m_curves.size() )
	{
		return NULL;
	}
	return m_curves[ iCurveIndex ] ;
}

void CCurveManager::AddCurve( CCurveBase* pCurve)
{
	if( pCurve != NULL )
	{
		pCurve->UpdateCurveData();
		pCurve->SetCurveID((UINT)m_curves.size() );
		m_curves.push_back( pCurve );	
	}
}

LONG CCurveManager::AddCurve(CCurveBase* pCurve, CCurveDataSource* pDataSource,CRulerCtrl *pRulerX,CRulerCtrl* pRulerY )
{
	if( pCurve != NULL )
	{
		if( pCurve->CreateCurve(m_pCurvePanel,pDataSource,pRulerX,pRulerY) )
		{
			pCurve->UpdateCurveData();
			pCurve->SetCurveID( m_lCurveID );
			m_curves.push_back ( pCurve );
			m_mapShowing[ m_lCurveID ] = TRUE;
			m_lCurveID ++;
			return m_lCurveID - 1;
		}
	}
	return -1;
}
int CCurveManager::GetCurvesCount()
{
	return m_curves.size();
}

void CCurveManager::UpdateCurvesPoints()
{
	for(int i = 0 ; i < (int)m_curves.size() ; i ++)
	{
		m_curves[i]->UpdateCurvePoints();
	}
}

void CCurveManager::UpdateCurvesData()
{
	for(int i = 0 ; i < (int)m_curves.size() ; i ++)
	{
		m_curves[i]->UpdateCurveData();
	}
}

void CCurveManager::CreateCurveManager( CCurvePanel* pCurvePanel )
{
	m_pCurvePanel = pCurvePanel;
}

void CCurveManager::ClearAllCurves( BOOL bDeleteMem/*=TRUE*/ )
{
	if( bDeleteMem )
	{
		for(int i = 0 ; i < (int)m_curves.size() ; i ++)
		{
			CCurveBase *pCurve = m_curves[i];
			delete pCurve;
			pCurve = NULL;
		}
	}
	m_curves.clear();
}

void CCurveManager::UpdateOtherCurvesPoints( CRulerCtrl* oneRulerY )
{
	if( oneRulerY == NULL )
	{
		return ;
	}
	for(int i = 0 ; i < (int)m_curves.size() ; i ++)
	{
		CCurveBase* pCurve = m_curves[i];
		if( pCurve->GetRulerY() == oneRulerY )
		{
			pCurve->UpdateCurvePoints();
		}
	}
}

void CCurveManager::UpdateOtherCurvesPoints( UINT nRulerYCtrlID )
{
	for(int i = 0 ; i < (int)m_curves.size() ; i ++)
	{
		CCurveBase* pCurve = m_curves[i];
		if( pCurve->GetRulerY()->GetDlgCtrlID() == nRulerYCtrlID )
		{
			pCurve->UpdateCurvePoints();
		}
	}
}
void CCurveManager::UpdateOtherCurvesData(CRulerCtrl* oneRulerY)
{
	if( oneRulerY == NULL )
	{
		return ;
	}
	for(int i = 0 ; i < (int)m_curves.size() ; i ++)
	{
		CCurveBase* pCurve = m_curves[i];
		if( pCurve->GetRulerY() == oneRulerY )
		{
			pCurve->UpdateCurveData();
		}
	}
}

void CCurveManager::UpdateOtherCurvesData( UINT nRulerYCtrlID )
{
	for(int i = 0 ; i < (int)m_curves.size() ; i ++)
	{
		CCurveBase* pCurve = m_curves[i];
		if( pCurve->GetRulerY()->GetDlgCtrlID() == nRulerYCtrlID )
		{
			pCurve->UpdateCurveData();
		}
	}
}
void CCurveManager::GetCurves( vector<CCurveBase*> &curves,CRulerCtrl* oneRulerY )
{
	curves.clear();
	for(int iCur = 0 ; iCur < (int)m_curves.size() ; iCur ++)
	{
		if( m_curves[iCur]->GetRulerY() == oneRulerY )
		{
			curves.push_back( m_curves[iCur] );
		}
	}
}

void CCurveManager::ShowTheCurve( int iCurveID , BOOL bShow)
{
	if( iCurveID < 0 || iCurveID >= (int)m_curves.size() )
	{
		return;
	}
	for(int i = 0 ; i < (int)m_curves.size() ; i ++)
	{
		CCurveBase* pCurve = m_curves[i];
		if( pCurve->GetCurveID() == iCurveID )
		{
			pCurve->ShowCurve( bShow, TRUE );
			break;
		}
	}
}

BOOL CCurveManager::GetCurveShowing( int iCurveID )
{
	if( iCurveID < 0 || iCurveID >= (int)m_curves.size() )
	{
		return FALSE;
	}
	for(int i = 0 ; i < (int)m_curves.size() ; i ++)
	{
		CCurveBase* pCurve = m_curves[i];
		if( pCurve->GetCurveID() == iCurveID )
		{
			return pCurve->GetShowing() ;
		}
	}
	return FALSE;
}
}}